package com.wonder.app.controller;


import com.wonder.config.MqttReceiveConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/receive")
public class ReceiveController {

    @Autowired
    private MqttReceiveConfig client;

    @Value("${spring.mqtt.client.id}")
    private String clientId;

    @RequestMapping("/connect")
    @ResponseBody
    public String connect(){
        client.connect();
        return clientId + "连接到服务器";
    }

    @RequestMapping("/disConnect")
    @ResponseBody
    public String disConnect(){
        client.disConnect();
        return clientId + "与服务器断开连接";
    }

    /**
     * 订阅某个主题
     * @param topic
     * @param qos
     * @return
     */
    @RequestMapping("subscribe")
    @ResponseBody
    public boolean subscribe(String topic,int qos){
        boolean subscribe = client.subscribe(topic, qos);
        return subscribe;
    }
}
